package com.tsu.mall.admin.controller;


import com.tsu.mall.common.VO.LayuiTableVO;
import com.tsu.mall.common.VO.ResultVO;
import com.tsu.mall.admin.entity.Attendant;
import com.tsu.mall.admin.service.IAttendantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 *  实现管理员表的维护
 * </p>
 *
 * @author tttttt
 * @since 2023-03-17
 */
@Controller
@RequestMapping("/attendant")
public class AttendantController {
@Autowired
private IAttendantService iAttendantService;
    @GetMapping("/attendantPage")
    public String attendantPage(){
        return "attendant";
    }

    @GetMapping("/list")
    @ResponseBody
    public LayuiTableVO list(Integer page,Integer limit,String keyword) {
        return iAttendantService.doList(page,limit,keyword);
    }

    //返回session中的值
    @PostMapping("/sessionValue")
    @ResponseBody
    public LayuiTableVO sessionValue(){
        return iAttendantService.sessionValue();
    }

    @PostMapping("/updatePassword")
    @ResponseBody
    public LayuiTableVO updatePassword(String oldPassword,String newPassword){
        return iAttendantService.doUpdatePassword(oldPassword,newPassword);
    }

    @PostMapping("/trans")
    @ResponseBody
    public LayuiTableVO trans(String transAttendantName){
        return iAttendantService.doTrans(transAttendantName);
    }

    @PostMapping("/doChose")
    @ResponseBody
    public LayuiTableVO doChose(Integer choseId,Integer attendantId){
        return iAttendantService.doChose(choseId,attendantId);
    }

    //上传头像功能
    @PostMapping("/avatar")
    @ResponseBody
    public LayuiTableVO avatar(MultipartFile file){
        String fileName=System.currentTimeMillis()+".jpg";
        File f=new File("D:/test/"+fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        LayuiTableVO vo=new LayuiTableVO();

        String s="/upload/"+fileName;
        Map<String ,String> map=new HashMap<>();
        map.put("avatar",s);
        vo.setCode(0);
        vo.setData(map);
        return vo;
    }
    @PostMapping("/save")
    @ResponseBody
    public ResultVO save(Attendant attendant){
        return iAttendantService.doSave(attendant);
    }

    @PostMapping("/selectById")
    @ResponseBody
    public LayuiTableVO selectById(Integer id){
        return iAttendantService.selectById(id);
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResultVO delete(Integer id){
        return iAttendantService.deleteById(id);
    }
}
